<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入检查项名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="要点" prop="spec">
        <el-input
          v-model="queryParams.spec"
          placeholder="请输入检查要点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        v-hasPermi="['labsafety:labsafetyCheckIndexItem:edit']">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="labsafetyCheckIndexItemList" row-key="id" default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="编号"  prop="itemNo" width="200"/>
      <el-table-column label="名称"  prop="name" width="200"/>
      <el-table-column label="要点"  prop="spec" />
      <el-table-column label="状态" align="center" prop="statusOpt" :formatter="statusOptFormat" width="80"/>
      <el-table-column label="操作" align="center" width="160" v-if="isPermit('labsafety:labsafetyCheckIndexItem:edit')">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['labsafety:labsafetyCheckIndexItem:edit']"
          >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加或修改安全检查指标项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="460px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="68px">
        <el-form-item label="父节点" prop="parentId" v-if="form.parentId && form.parentId!=0">
          <treeselect :default-expand-level="1" v-model="form.parentId" :options="parentOptions" :normalizer="normalizer" placeholder="选择父节点" class="input_w"/>
        </el-form-item>
        <el-form-item label="编号" prop="itemNo">
          <el-input v-model="form.itemNo" placeholder="请输入检查项编号" class="input_w"/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入检查项名称" class="input_w"/>
        </el-form-item>
        <el-form-item label="要点" prop="spec">
          <el-input v-model="form.spec" placeholder="请输入检查要点" class="input_w"/>
        </el-form-item>
        <el-form-item label="状态" prop="statusOpt">
          <el-select v-model="form.statusOpt" placeholder="请选择状态" class="input_w">
            <el-option v-for="dict in statusOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-hasPermi="['labsafety:labsafetyCheckIndexItem:edit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLabsafetyCheckIndexItem, getLabsafetyCheckIndexItem,
delLabsafetyCheckIndexItem, addLabsafetyCheckIndexItem,
updateLabsafetyCheckIndexItem,
exportLabsafetyCheckIndexItem,
listExcludeChild,} from "@/api/labsafety/labsafetyCheckIndexItem";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "LabsafetyCheckIndexItem",
  components:{
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      indexId:1,
      // 安全检查指标项表格数据
      labsafetyCheckIndexItemList: [],
      // 状态字典
      statusOptOptions: [],
      // 部门树选项
      parentOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        indexId: 1,
      },
      // 表单校验
      rules: {
       /* name: [{
          required: true,
          message: "名称不能为空",
          trigger: "blur"
        }], */
        itemNo: [{
          required: true,
          message: "编号不能为空",
          trigger: "blur"
        }],
        statusOpt: [{
          required: true,
          message: "状态不能为空",
          trigger: "change"
        }],
      }
    };
  },
  created() {
    this.indexId =  this.$route.params && this.$route.params.indexId;
    this.queryParams.indexId = this.indexId;
    this.form.indexId = this.indexId;
    this.getDicts("statusOpt").then(response => {
      this.statusOptOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询安全检查指标项列表 */
    getList() {
      this.loading = true;
      listLabsafetyCheckIndexItem(this.queryParams).then(response => {
        this.labsafetyCheckIndexItemList = this.handleTree(response.data, "id");
        this.loading = false;
      });
    },
    // 状态字典翻译
    statusOptFormat(row, column) {
      return this.selectDictLabel(this.statusOptOptions, row.statusOpt);
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        indexId: this.form.indexId, // 默认值
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.indexId = this.indexId;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.id;
      }
      this.form.statusOpt = 1;
      this.open = true;
      this.title = "添加安全检查指标项";
      listLabsafetyCheckIndexItem({indexId:this.indexId}).then(response => {
        this.parentOptions = this.handleTree(response.data, "id");
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLabsafetyCheckIndexItem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改安全检查指标项";
      });
      listExcludeChild({indexId:this.indexId, id:id}).then(response => {
          this.parentOptions = this.handleTree(response.data, "id");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLabsafetyCheckIndexItem(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLabsafetyCheckIndexItem(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除安全检查指标项编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLabsafetyCheckIndexItem(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    isPermit(permitKey){
      if(this.$store.state.user.name=='admin'){
        return true;
      }
      let  permissions = this.$store.getters.permissions;
      return permissions.includes(permitKey);
    },
  }
};
</script>
<style type="text/css" scoped lang="scss">
.input_w{
  width: 260px;
}
</style>
